package com.dragon.shop_order.service.impl;

import com.dragon.shop_common.utils.AjaxResult;
import com.dragon.shop_order.entity.Order;
import com.dragon.shop_order.mapper.OrderMapper;
import com.dragon.shop_order.service.IOrderService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author Baomidou
 * @since 2025-03-20
 */
@Service
public class OrderServiceImpl extends ServiceImpl<OrderMapper, Order> implements IOrderService {

    @Resource
    private OrderMapper orderMapper;

    @Override
    public AjaxResult<String> createOrder(String userId) {
        // 尝试直接拒绝，不抛出异常
        // if ("1".equals(userId)) {
        //     return AjaxResult.failed();
        // }

        Order order = new Order();
        order.setUserId(Long.parseLong(userId));
        order.setOrderNo(String.valueOf(System.currentTimeMillis()));
        boolean flag =orderMapper.insert(order) > 0;
        if (!flag) {
            return AjaxResult.failed("订单创建失败");
        }

        return AjaxResult.succeedWith("订单创建成功，订单号：" + order.getOrderNo());
    }
}
